package com.crm.services;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.model.jdao.IRoleDao;
import com.crm.model.mdao.PowerandroleMapper;
import com.crm.model.mdao.RoleMapper;
import com.crm.model.pojo.Role;
import com.github.pagehelper.PageHelper;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServices {
	
	@Autowired
	IRoleDao iDao;
	@Autowired
	RoleMapper roleMapper;
	
	@Autowired
	PowerandroleMapper PowerandroleMapper;
	
	
	public Iterable<Role> findAllRole() {
		Iterable<Role> findAll = iDao.findAll();
		return findAll;
	}
	
	/**
	 * 分页查询
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(Integer pageNum, Integer pageSize,Integer statusid,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		
		System.out.println("看看"+statusid);
		//获取
		if(statusid!=null)
		if(statusid == 0) {
			statusid=null;
		}
		List<Map<String,String>> list = roleMapper.selectByAll(statusid,search);
		System.out.println("大小" + list.size());
		return list;
	}
	
	/**
	 * 添加角色
	 * @param user
	 * @return
	 */
	public String save(Role role,List<Integer> limap) {
		Role save = iDao.save(role);
		save.getRoleId();
		for (Integer integer : limap) {
			if(integer==null) {
				continue;
			}
			com.crm.model.mpojo.Powerandrole pwr=new com.crm.model.mpojo.Powerandrole();
			pwr.setpId(integer);
			pwr.setRoleId(save.getRoleId());
			PowerandroleMapper.insertSelective(pwr);
		}
		return "";
	}
	
	/**
	 * 修改角色
	 * @param role
	 * @param limap
	 */
	public void updateRole(com.crm.model.mpojo.Role role, List<Integer> limap) {
		roleMapper.updateByPrimaryKeySelective(role);
		PowerandroleMapper.deleteByRoleId(role.getRoleId());
		for (Integer integer : limap) {
			if(integer==null) {
				continue;
			}
			com.crm.model.mpojo.Powerandrole pwr=new com.crm.model.mpojo.Powerandrole();
			pwr.setpId(integer);
			pwr.setRoleId(role.getRoleId());
			PowerandroleMapper.insertSelective(pwr);
		}
		
	}
	
	/**
	 * 查询所有角色Map<lable,value>,用于前台界面筛选。
	 * @return
	 */
	public List<Map<String,String>> selectAllRoleView(){
		return roleMapper.selectAllRoleView();
	}
	
	

}
